package com.alatus.config.handler;

import com.alatus.result.CodeEnum;
import com.alatus.result.Result;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
//拦截标注了@RestController的所有controller的方法
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public Result handlerException(Exception e) {
        //打出异常信息
        e.printStackTrace();
//    权限不足的异常
        if(e.getMessage().equals("Access Denied")){
            return Result.FAIL(CodeEnum.ACCESS_DENIED);
        }
        else{
            return Result.FAIL(e.getMessage());
        }
    }

//    数据库的异常
    @ExceptionHandler(value = DataAccessException.class)
    public Result handlerSQLException(DataAccessException e) {
        e.printStackTrace();
        return Result.FAIL(CodeEnum.DATA_ACCESS_EXCEPTION);
    }
}
